package com.hxx.day5;

import lombok.Data;
import org.junit.Test;

import java.io.*;
import java.util.*;

public class TestDemo3 {
    /**
     * 泛型的应用
     */
    @Test
    public void test1() {
        A<Integer,String> a = new A<>();
        a.setId(1);
        a.setName("张三");
    }

    @Test
    public void test2() {
        //null可以作为key
        Map<String,String> map = new HashMap<>();
        map.put(null,"张三");
        //抛异常,null不可以可以作为key
        Hashtable<String, String> hashtable = new Hashtable<>();
        hashtable.put(null,"张三");
    }

    /**
     * 属性文件
     * 资源文件
     */
    @Test
    public void test3() {
        Properties properties = new Properties();
        //添加
        properties.setProperty("name","张三");
        properties.put("age", "18");
        // 获取
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.get("age"));
    }

    /**
     * 读取文件
     * @throws Exception
     */
    @Test
    public void test4()throws  Exception {
        //读进来：输入流，写出去：输出流
        InputStream in = new FileInputStream("d:/zmx/hxx.txt");
        //创建一个Properties对象
        Properties properties = new Properties();
        //加载文
        properties.load(in);
        //获取
        System.out.println(properties.getProperty("id"));
        System.out.println(properties.getProperty("username"));
        System.out.println(properties.getProperty("sex"));
    }
    /**
     * 内容写入文件
     */
    @Test
    public void test5()throws  Exception {
        Properties properties = new Properties();
        //添加内容
        properties.setProperty("id","1");
        properties.setProperty("username","tom");
        properties.setProperty("sex","boy");
        //写入文件
        OutputStream out = new FileOutputStream("d:/zmx/hxx1.txt");
        properties.store(out,"desc");
    }

    /**
     * 读取目录中的所有文件
     */
    @Test
    public void test6()throws Exception {
        //指定目录
        File file = new File("d:/zmx/db");
        File[] files = file.listFiles();
        for (File f : files) {
            //读进来：输入流，写出去：输出流
            InputStream in = new FileInputStream(f);
            //创建一个Properties对象
            Properties properties = new Properties();
            //加载文
            properties.load(in);
            //获取
            System.out.println(properties.getProperty("id"));
            System.out.println(properties.getProperty("username"));
            System.out.println(properties.getProperty("sex"));
            System.out.println("------------------");
        }
    }
}

@Data
class A<T,T1>{
    private T id;
    private T1 name;
}


